﻿

#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Design;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
using FGame = Freeform.Game;
using XGame = Microsoft.Xna.Framework.Game;
using FComponent = Freeform.Component;

#endregion


namespace Freeform
{
    /// <summary>
    /// The camera controls where the entities appear in the viewport.
    /// </summary>
    public class Camera3D : Component, IGeometric3D
    {

        #region Members

        /// <summary>
        /// Gets the viewport that this camera belongs to.
        /// </summary>
        public virtual Viewport3D Viewport { get; private set; }


        /// <summary>
        /// Gets the world that this camera is observing.
        /// </summary>
        public virtual World World { get; private set; }


        /// <summary>
        /// Gets or sets the position of the camera.
        /// </summary>
        public Vector3 Position { get; set; }


        /// <summary>
        /// Gets or sets the orientation of the camera.
        /// </summary>
        public Vector3 Orientation { get; set; }


        /// <summary>
        /// Gets or sets how much fits on the camera.
        /// </summary>
        public Vector3 Size { get; set; }


        public Single AspectRatio { get; set; }
        public Single FieldOfView { get; set; }
        public Single NearPlane { get; set; }
        public Single FarPlane { get; set; }
        public Vector3 LookAt { get; set; }
        public Matrix ViewMatrix { get; private set; }
        public Matrix ProjectionMatrix { get; private set; }
        public Vector3 LightDirection { get; set; }
        public Single AmbientLight { get; set; }

        #endregion


        #region Init

        /// <summary>
        /// Creates a new instance of a camera.
        /// </summary>
        /// <param name="Viewport">The viewport that this camera belongs to.</param>
        /// 
        //
        public Camera3D(Viewport3D Viewport)
            : base(Viewport.World.ComponentList)
        {
            this.Viewport = Viewport;
            World = Viewport.World;
            IsEnabled = true;
            Viewport.Camera = this;
            LightDirection = new Vector3(1, -1, -1);
        }

        #endregion


        #region Update

        public override void Update(GameTime Time)
        {
            base.Update(Time);

            ViewMatrix = Matrix.CreateLookAt(Position, LookAt, Orientation);
            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                FieldOfView,
                AspectRatio,
                NearPlane,
                FarPlane);
        }

        #endregion


        #region Utility


        #endregion

    }
}